package org.smartercontextmobile.mobileapp;

import org.smartercontext.contextmger.json.writer.IJSONWriter;
import org.smartercontext.contextmger.json.writer.JSONWriter;
import org.smartercontextmobile.communication.Communication;
import org.smartercontextmobile.communication.ICommunication;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;

public class LocalizationActivity extends ActionBarActivity {

	private String userName = "";
	private LocationManager locManager;
	private LocationListener locListener;
	private TextView latitude;
	private TextView longitude;
	private TextView precision;
	private TextView statusProv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_localization);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
			
		}
		
		Intent intent = getIntent();
		userName = intent.getStringExtra(LoginActivity.EXTRA_MESSAGE);
		
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.localization, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_localization,
					container, false);
			return rootView;
		}
	}
	
	public void activeLocalization(View v){
		
		
		
		locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		Location loc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		showPosition(loc);
		
		locListener = new LocationListener() {
	    	public void onLocationChanged(Location location) {
	    		showPosition(location);
	    	}
	    	public void onProviderDisabled(String provider){
	    		statusProv.setText("Provider OFF");
	    	}
	    	public void onProviderEnabled(String provider){
	    		statusProv.setText("Provider ON ");
	    	}
	    	public void onStatusChanged(String provider, int status, Bundle extras){
	    		Log.i("", "Provider Status: " + status);
	    		statusProv.setText("Provider Status: " + status);
	    	}
    	};
    	
    	locManager.requestLocationUpdates(
    			LocationManager.GPS_PROVIDER, 30000, 0, locListener);
		
	}
	
	public void showPosition(Location loc){
		
		latitude = (TextView)findViewById(R.id.lbl_latitude_id);
		longitude = (TextView)findViewById(R.id.lbl_longitude_id);
		precision = (TextView)findViewById(R.id.lbl_precision_id);
		statusProv = (TextView)findViewById(R.id.lbl_provstaus_id);
		
		if(loc!=null){
			latitude.setText("Latitude: " + String.valueOf(loc.getLatitude()));
    		longitude.setText("Longitude: " + String.valueOf(loc.getLongitude()));
    		precision.setText("Precision: " + String.valueOf(loc.getAccuracy()));
    		Log.i("", String.valueOf(loc.getLatitude() + " - " + String.valueOf(loc.getLongitude())));
		}else{
			latitude.setText("Latitude: (no data)");
    		longitude.setText("Longitude: (no data)");
    		precision.setText("Precision: (no data)");
		}
	}
	
	public void sendLocalization (View v){
		TextView tVLatitude = (TextView)findViewById(R.id.lbl_latitude_id);
		TextView tVLongitude = (TextView)findViewById(R.id.lbl_longitude_id);
		
		if(tVLatitude.getText().toString()!="" & tVLongitude.getText().toString()!=""
				&!tVLatitude.getText().toString().contains("no data")&!tVLongitude.getText()
				.toString().contains("no data")){
		
		String latitude = tVLatitude.getText().toString().substring(10);
		String longitude = tVLongitude.getText().toString().substring(11);
		
		IJSONWriter writer = new JSONWriter();
		String JSONLoc = writer.writeLocation(latitude, longitude, userName);
		ICommunication comm = new Communication();
		comm.sendCurrentlocation(JSONLoc);
		}
	}
	
	public void disableLocalization (View v){
		
		if(locManager!=null && locListener!=null){
		
		locManager.removeUpdates(locListener);
		
		}
	}
	
	public void logout (View v){
		
		if(locManager!=null && locListener!=null){
			
			locManager.removeUpdates(locListener);
			
			}
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
		finish();
		
	}

}
